package com.yanqu.road.server.manager.activity.doublespring;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.activity.doublespring.DoubleSpringUserDataDaoImpl;
import com.yanqu.road.dao.impl.activity.doublespring.DoubleSpringUserInviteDataDaoImpl;
import com.yanqu.road.dao.impl.activity.doublespring.DoubleSpringZlUserDataDaoImpl;
import com.yanqu.road.entity.activity.doublespring.DoubleSpringActivityConfig;
import com.yanqu.road.entity.activity.doublespring.data.DoubleSpringUserData;
import com.yanqu.road.entity.activity.doublespring.data.DoubleSpringUserInviteData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.util.CompositeKey;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DoubleSpringProto;
import com.yanqu.road.server.handle.CrossServerHandler;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.doublespring.pb.CrossDoubleSpringActivityPb;
import com.yanqu.road.server.manager.config.GoodsMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CrossDoubleSpringActivity {

    private static Logger logger = LogManager.getLogger(CrossDoubleSpringActivity.class.getName());

    //活动配置信息
    private DoubleSpringActivityConfig doubleSpringActivityConfig;

    //玩家数据
    private Map<Long, DoubleSpringUserData> userDataMap = new ConcurrentHashMap<>();

    //玩家邀请数据.k:邀请人id，v：被邀请人map
    private Map<Long, Map<Long, DoubleSpringUserInviteData>> userInviteDataMap = new ConcurrentHashMap<>();

    //玩家邀请数据.k:被邀请人id，v：邀请人map
    private Map<Long, Map<Long, DoubleSpringUserInviteData>> userBeInviteDataMap = new ConcurrentHashMap<>();

    //被删除的数据
    private List<DoubleSpringUserInviteData> delInviteLst = new ArrayList<>();

    //2个同行人的锁MAP
    private final ConcurrentHashMap<CompositeKey, Lock> locks = new ConcurrentHashMap<>();

    /**
     * 存储数据
     */
    public void save() {
        //玩家数据
        for (DoubleSpringUserData data : this.userDataMap.values()) {
            if (data.isInsertOption()) {
                new DoubleSpringUserDataDaoImpl().add(data);
            } else if (data.isUpdateOption()) {
                new DoubleSpringUserDataDaoImpl().update(data);
            }
        }
        //玩家邀请数据
        for (Map<Long, DoubleSpringUserInviteData> userMap : this.userInviteDataMap.values()) {
            for (DoubleSpringUserInviteData data : userMap.values()) {
                if (data.isInsertOption()) {
                    new DoubleSpringUserInviteDataDaoImpl().add(data);
                } else if (data.isUpdateOption()) {
                    new DoubleSpringUserInviteDataDaoImpl().update(data);
                }
            }
        }
        //被删除的数据
        List<DoubleSpringUserInviteData> delList;
        synchronized (delInviteLst) {
            delList = new ArrayList<>(delInviteLst);
            delInviteLst.clear();
        }
        for (DoubleSpringUserInviteData data : delList) {
            if (data.isUpdateOption()) {
                new DoubleSpringUserInviteDataDaoImpl().update(data);
            }
        }
    }

    /**
     * 初始化数据
     * @param activityId
     */
    private void initData(int activityId) {
        //玩家数据
        this.userDataMap = new DoubleSpringUserDataDaoImpl().getData(activityId);
        //邀请数据
        List<DoubleSpringUserInviteData> list = new DoubleSpringUserInviteDataDaoImpl().getData(activityId);
        for (DoubleSpringUserInviteData data : list) {
            //邀请列表
            Map<Long, DoubleSpringUserInviteData> inviteMap = this.userInviteDataMap.computeIfAbsent(data.getUserId(), k -> new ConcurrentHashMap<>());
            inviteMap.put(data.getInviteUserId(), data);
            //被邀请列表
            Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.computeIfAbsent(data.getInviteUserId(), k -> new ConcurrentHashMap<>());
            beInviteMap.put(data.getUserId(), data);
        }
    }

    /**
     * 构造方法
     * @param doubleSpringActivityConfig
     */
    public CrossDoubleSpringActivity(DoubleSpringActivityConfig doubleSpringActivityConfig) {
        this.doubleSpringActivityConfig = doubleSpringActivityConfig;
        //加载数据
        this.initData(doubleSpringActivityConfig.getActivityInfo().getActivityId());
    }

    /**
     * 刷新config
     * @param doubleSpringActivityConfig
     */
    public void refreshConfig(DoubleSpringActivityConfig doubleSpringActivityConfig) {
        this.doubleSpringActivityConfig = doubleSpringActivityConfig;
    }

    /**
     * 获取配置
     * @return
     */
    public DoubleSpringActivityConfig getDoubleSpringActivityConfig() {
        return doubleSpringActivityConfig;
    }

    /**
     * 邀请玩家
     * @param userId
     * @param beInviteUserId
     * @param beInviteServerId
     * @throws BusinessException
     */
    public synchronized void inviteUser(long userId, long beInviteUserId, long beInviteServerId) throws BusinessException {
        //邀请人是否组队
        DoubleSpringUserData userData = this.userDataMap.get(userId);
        if (userData != null && userData.getPartnerUserId() > 0) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_SELF_ALREADY_JOIN_TEAM);
        }
        //被邀请人是否组队
        DoubleSpringUserData beUserData = this.userDataMap.get(beInviteUserId);
        if (beUserData != null && beUserData.getPartnerUserId() > 0) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_TARGET_ALREADY_JOIN_TEAM);
        }
        //获取玩家邀请列表
        Map<Long, DoubleSpringUserInviteData> inviteMap = this.userInviteDataMap.get(userId);
        //判断邀请数据是否达到上限
        if (inviteMap != null && inviteMap.size() >= this.doubleSpringActivityConfig.getDoubleSpringInviteLimit()) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_INVITE_LIMIT);
        }
        //判是否已经存在在邀请列表中
        if (inviteMap != null && inviteMap.containsKey(beInviteUserId)) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_TARGET_ALREADY_IN_INVITE_LIST);
        }
        //判断被邀请数据是否已经达到上限
        Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.get(beInviteUserId);
        if (beInviteMap != null && beInviteMap.size() >= this.doubleSpringActivityConfig.getDoubleSpringInviteLimit()) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_BE_INVITE_LIMIT);
        }
        //添加邀请数据
        this.addInviteUser(userId, beInviteUserId);

        //判断被邀请的玩家是否有baseInfo，如果没有，去区服查询存储在跨服
        UserBaseInfo beInviteUserBaseInfo = CrossUserMgr.getUserBaseInfo(beInviteUserId);
        if (beInviteUserBaseInfo == null) {
            //玩家信息不存在，去区服查询
            DoubleSpringProto.DoubleSpringCrossQueryUserReqMsg.Builder builder = DoubleSpringProto.DoubleSpringCrossQueryUserReqMsg.newBuilder();
            builder.setActivityId(this.doubleSpringActivityConfig.getActivityInfo().getActivityId());
            builder.setUserId(0);
            builder.setServerId(0);
            builder.setQueryUserId(beInviteUserId);
            builder.setQueryUserAliasId(0);
            builder.setQueryServerId(beInviteServerId);
            MessageHelper.sendPacket(beInviteServerId, 0, YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_CROSS_QUERY_USER_BASE, builder));
        }

        //同步被邀请列表长度
        this.syncBeInviteSize(beInviteUserId, beInviteServerId);
    }

    /**
     * 添加邀请玩家
     * @param userId
     * @param beInviteUserId
     */
    private void addInviteUser(long userId, long beInviteUserId) {
        DoubleSpringUserInviteData data = new DoubleSpringUserInviteData();
        data.setActivityId(this.doubleSpringActivityConfig.getActivityInfo().getActivityId());
        data.setUserId(userId);
        data.setInviteUserId(beInviteUserId);
        data.setDel(0);
        data.setCreateTime(System.currentTimeMillis());
        data.setInsertOption();
        //put 邀请数据
        Map<Long, DoubleSpringUserInviteData> inviteMap = this.userInviteDataMap.computeIfAbsent(userId, k -> new ConcurrentHashMap<>());
        inviteMap.put(beInviteUserId, data);
        this.userInviteDataMap.put(userId, inviteMap);
        //put被邀请数据
        Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.computeIfAbsent(beInviteUserId, k -> new ConcurrentHashMap<>());
        beInviteMap.put(userId, data);
        this.userBeInviteDataMap.put(beInviteUserId, beInviteMap);
    }

    /**
     * 取消邀请
     * @param userId
     * @param beInviteUserId
     */
    public synchronized DoubleSpringUserInviteData cancelInvite(long userId, long beInviteUserId) {
        DoubleSpringUserInviteData data = null;
        //删除邀请记录
        Map<Long, DoubleSpringUserInviteData> inviteMap = this.userInviteDataMap.get(userId);
        if (inviteMap != null) {
            data = inviteMap.get(beInviteUserId);
            if (data != null) {
                inviteMap.remove(beInviteUserId);
                //删除数据
                this.delInviteData(data);
            }
        }
        //删除被邀请记录
        Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.get(beInviteUserId);
        if (beInviteMap != null) {
            beInviteMap.remove(userId);
        }
        //同步被邀请列表长度
        this.syncBeInviteSize(beInviteUserId);
        //返回
        return data;
    }

    /**
     * 同步被邀请列表长度
     * @param userId
     */
    private void syncBeInviteSize(long userId) {
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
        if (userBaseInfo != null) {
            this.syncBeInviteSize(userId, userBaseInfo.getServerId());
        }
    }

    /**
     * 同步被邀请列表长度
     * @param userId
     * @param serverId
     */
    private void syncBeInviteSize(long userId, long serverId) {
        DoubleSpringProto.DoubleSpringBeInviteSizeSyncMsg.Builder builder = DoubleSpringProto.DoubleSpringBeInviteSizeSyncMsg.newBuilder();
        builder.setBeInviteSize(this.userBeInviteDataMap.get(userId) == null ? 0 : this.userBeInviteDataMap.get(userId).size());
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_DOUBLE_SPRING_SYNC_BE_INVITE_SIZE, builder);
        MessageHelper.sendPacket(serverId, userId, message);
    }


    /**
     * 取消所有邀请
     * @param userId 玩家ID
     * @return 玩家的邀请信息
     */
    public synchronized Map<Long, DoubleSpringUserInviteData> cancelInviteAll(long userId) {
        return this.delAllInviteData(userId);
    }

    /**
     * 删除数据
     * @param data
     */
    private void delInviteData(DoubleSpringUserInviteData data) {
        data.setDel(1);
        this.delInviteLst.add(data);
    }

    /**
     * 同意邀请
     * @param activityId
     * @param userId
     * @param inviteUserId
     * @param serverId
     * @param inviteServerId
     * @throws BusinessException
     */
    public synchronized void agreeInvite(int activityId, long userId, long inviteUserId, long serverId, long inviteServerId) throws BusinessException {
        //被邀请人是否组队
        DoubleSpringUserData userData = this.userDataMap.get(userId);
        if (userData != null && userData.getPartnerUserId() > 0) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_SELF_ALREADY_JOIN_TEAM);
        }
        //邀请人是否组队
        DoubleSpringUserData userData2 = this.userDataMap.get(inviteUserId);
        if (userData2 != null && userData2.getPartnerUserId() > 0) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_TARGET_ALREADY_JOIN_TEAM);
        }
        //判断邀请信息是否存在
        Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.get(userId);
        if (!beInviteMap.containsKey(inviteUserId)) {
            throw BusinessException.newException(GameErrorCode.E_DOUBLE_SPRING_INVITE_NOT_EXIST);
        }

        //被邀请人
        userData = this.getAndCreateUserData(userId, serverId);
        //邀请人
        userData2 = this.getAndCreateUserData(inviteUserId, inviteServerId);

        //更新被邀请人信息
        userData.setPartnerUserId(inviteUserId);
        userData.setPartnerUserServerId(inviteServerId);
        //添加积分
        userData.addScore(userData2.getScore(), false);
        //通知区服分数变更,组队成功
        DoubleSpringProto.DoubleSpringCrossTeamSuccessSyncMsg.Builder selfBuilder = DoubleSpringProto.DoubleSpringCrossTeamSuccessSyncMsg.newBuilder();
        selfBuilder.setActivityId(activityId);
        selfBuilder.setScore(userData.getTotalScore());
        selfBuilder.setPartnerUserId(userData.getPartnerUserId());
        selfBuilder.setAddScore(userData2.getScore());
        selfBuilder.setOperateFlag(true);
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_TEAM_SUCCESS_FROM_CROSS, selfBuilder));

        //更新邀请人信息
        userData2.setPartnerUserId(userId);
        userData2.setPartnerUserServerId(serverId);
        //添加积分
        userData2.addScore(userData.getScore(), false);
        //通知区服分数变更,组队成功
        DoubleSpringProto.DoubleSpringCrossTeamSuccessSyncMsg.Builder partnerBuilder = DoubleSpringProto.DoubleSpringCrossTeamSuccessSyncMsg.newBuilder();
        partnerBuilder.setActivityId(activityId);
        partnerBuilder.setScore(userData2.getTotalScore());
        partnerBuilder.setPartnerUserId(userData2.getPartnerUserId());
        partnerBuilder.setAddScore(userData.getScore());
        partnerBuilder.setOperateFlag(false);
        MessageHelper.sendPacket(inviteServerId, inviteUserId, YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_TEAM_SUCCESS_FROM_CROSS, partnerBuilder));

        //删除2人的组队信息
        this.delAllInviteAndBeInvite(inviteUserId);
        this.delAllInviteAndBeInvite(userId);

        //同步一下伙伴信息
        this.syncUserData(userData.getUserId(), userData.getUserServerId(), false);
        this.syncUserData(userData2.getUserId(), userData2.getUserServerId(), false);
    }

    /**
     * 删除玩家的邀请，被邀请数据
     * @param userId
     */
    private void delAllInviteAndBeInvite(long userId) {
        this.delAllInviteData(userId);
        this.delAllBeInviteData(userId);
    }

    /**
     * 删除玩家所有的邀请信息
     * @param userId 玩家ID
     * @return 玩家的邀请信息
     */
    private Map<Long, DoubleSpringUserInviteData> delAllInviteData(long userId) {
        //删除邀请人的组队信息
        Map<Long, DoubleSpringUserInviteData> selfInviteMap = this.userInviteDataMap.get(userId);
        this.userInviteDataMap.remove(userId);
        if (selfInviteMap != null) {
            for (DoubleSpringUserInviteData data : selfInviteMap.values()) {
                this.delInviteData(data);
                //其他玩家的已邀请数据
                Map<Long, DoubleSpringUserInviteData> otherInviteMap = this.userBeInviteDataMap.get(data.getInviteUserId());
                if (otherInviteMap != null) {
                    otherInviteMap.remove(userId);
                    if (otherInviteMap.isEmpty()) {
                        this.userBeInviteDataMap.remove(data.getInviteUserId());
                    }
                }
                //同步被邀请列表长度
                this.syncBeInviteSize(data.getInviteUserId());
            }
        }
        return selfInviteMap;
    }

    /**
     * 删除玩家所有被邀请信息
     * @param userId
     */
    private void delAllBeInviteData(long userId) {
        //删除邀请人的被邀请信息
        Map<Long, DoubleSpringUserInviteData> selfBeInviteMap = this.userBeInviteDataMap.get(userId);
        this.userBeInviteDataMap.remove(userId);
        if (selfBeInviteMap != null) {
            for (DoubleSpringUserInviteData data : selfBeInviteMap.values()) {
                this.delInviteData(data);
                //其他玩家的已邀请数据
                Map<Long, DoubleSpringUserInviteData> otherInviteMap = this.userInviteDataMap.get(data.getUserId());
                if (otherInviteMap != null) {
                    otherInviteMap.remove(userId);
                    if (otherInviteMap.isEmpty()) {
                        this.userInviteDataMap.remove(data.getUserId());
                    }
                }
            }
        }
    }

    /**
     * 获取and创建玩家数据
     * @param userId
     * @param serverId
     * @return
     */
    private DoubleSpringUserData getAndCreateUserData(long userId, long serverId) {
        DoubleSpringUserData userData = this.userDataMap.get(userId);
        if (userData == null) {
            userData = new DoubleSpringUserData();
            userData.setActivityId(this.doubleSpringActivityConfig.getActivityInfo().getActivityId());
            userData.setUserId(userId);
            userData.setUserServerId(serverId);
            userData.setPartnerUserId(0);
            userData.setPartnerUserServerId(0);
            userData.setScore(0);
            userData.setTotalScore(0);
            userData.setCreateTime(System.currentTimeMillis());
            userData.setInsertOption();
            this.userDataMap.put(userData.getUserId(), userData);
        }
        return userData;
    }

    /**
     * 拒绝邀请
     * @param userId 被邀请的用户ID
     * @return 被邀请的用户邀请数据
     */
    public Map<Long, DoubleSpringUserInviteData> rejectInvite(long userId) {
        Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.get(userId);
        this.userBeInviteDataMap.remove(userId);
        if (beInviteMap != null) {
            for (DoubleSpringUserInviteData data : beInviteMap.values()) {
                this.delInviteData(data);
                //其他玩家的已邀请数据
                Map<Long, DoubleSpringUserInviteData> inviteDataMap = this.userInviteDataMap.get(data.getUserId());
                if (inviteDataMap != null) {
                    inviteDataMap.remove(userId);
                }
            }
        }
        return beInviteMap;
    }

    /**
     * 获取已邀请列表
     * @param userId
     * @return
     */
    public DoubleSpringProto.DoubleSpringGetInviteUserListRespMsg.Builder inviteList(long userId) {
        DoubleSpringProto.DoubleSpringGetInviteUserListRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringGetInviteUserListRespMsg.newBuilder();
        Map<Long, DoubleSpringUserInviteData> inviteMap = this.userInviteDataMap.get(userId);
        if (inviteMap != null) {
            for (long inviteUserId : inviteMap.keySet()) {
                //积分数据
                DoubleSpringUserData doubleSpringUserData = this.userDataMap.get(inviteUserId);
                //玩家基础数据
                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(inviteUserId);
                DoubleSpringProto.DoubleSpringPlayerData.Builder builder = CrossDoubleSpringActivityPb.buildDoubleSpringPlayerData(inviteUserId, doubleSpringUserData, userBaseInfo);
                respMsg.addPlayerData(builder);
            }
        }
        respMsg.setRet(0);
        return respMsg;
    }

    /**
     * 获取被邀请列表
     * @param userId
     * @return
     */
    public DoubleSpringProto.DoubleSpringGetReceivedInviteListRespMsg.Builder beInviteList(long userId) {
        DoubleSpringProto.DoubleSpringGetReceivedInviteListRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringGetReceivedInviteListRespMsg.newBuilder();
        Map<Long, DoubleSpringUserInviteData> beInviteMap = this.userBeInviteDataMap.get(userId);
        if (beInviteMap != null) {
            for (long inviteUserId : beInviteMap.keySet()) {
                //积分数据
                DoubleSpringUserData doubleSpringUserData = this.userDataMap.get(inviteUserId);
                //玩家基础数据
                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(inviteUserId);
                DoubleSpringProto.DoubleSpringPlayerData.Builder builder = CrossDoubleSpringActivityPb.buildDoubleSpringPlayerData(inviteUserId, doubleSpringUserData, userBaseInfo);
                respMsg.addPlayerData(builder);
            }
        }
        respMsg.setRet(0);
        return respMsg;
    }

    /**
     * 获取出行锁
     * @param userId1
     * @param userId2
     * @return
     */
    public Lock getLock(long userId1, long userId2) {
        //实例化复合键，无关userId顺序。
        CompositeKey key = new CompositeKey(userId1, userId2);
        return locks.computeIfAbsent(key, k -> new ReentrantLock());
    }

    /**
     * 出行
     * @param userId
     * @param serverId
     */
    public DoubleSpringUserData travel(int activityId, long userId, long serverId) {
        //获取玩家数据
        DoubleSpringUserData doubleSpringUserData = this.getAndCreateUserData(userId, serverId);
        //获取获得的积分
        long addScore = this.getTravelScore();

        //判断是否有伙伴
        if (doubleSpringUserData.getPartnerUserId() > 0) {
            //锁一下两个同行人
            Lock lock = this.getLock(userId, doubleSpringUserData.getPartnerUserId());
            lock.lock();
            try {
                //自己-添加积分
                doubleSpringUserData.addScore(addScore, true);
                //伙伴-添加积分
                DoubleSpringUserData partnerData = this.userDataMap.get(doubleSpringUserData.getPartnerUserId());
                if (partnerData != null) {
                    //伙伴-添加积分
                    partnerData.addScore(addScore, false);
                    //伙伴-添加积分回包
                    DoubleSpringProto.DoubleSpringCrossUpdateUserScoreSyncMsg.Builder partnerBuilder = DoubleSpringProto.DoubleSpringCrossUpdateUserScoreSyncMsg.newBuilder();
                    partnerBuilder.setActivityId(activityId);
                    partnerBuilder.setScore(partnerData.getTotalScore());
                    partnerBuilder.setPartnerUserId(partnerData.getPartnerUserId());
                    partnerBuilder.setAddScore(addScore);
                    partnerBuilder.setTravelFlag(false);
                    MessageHelper.sendPacket(partnerData.getUserServerId(), partnerData.getUserId(), YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_UPDATE_SCORE_FROM_CROSS, partnerBuilder));
                    //同步一下
                    this.syncUserData(partnerData.getUserId(), partnerData.getUserServerId(), false);
                }
            } finally {
                lock.unlock();
            }
        } else {
            //自己-添加积分
            doubleSpringUserData.addScore(addScore, true);
        }
        return doubleSpringUserData;
    }

    /**
     * 获取出行能获得的积分
     * @return
     */
    public long getTravelScore() {
        Property property = this.doubleSpringActivityConfig.getDoubleSpringItemCost();
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            int goodsId = entry.getKey();
            int num = entry.getValue().intValue();
            //获取道具详情
            GoodsInfo goodsInfo = GoodsMgr.getGoodsInfo(goodsId);
            if (goodsInfo != null) {
                return (long) goodsInfo.getParamList().get(0).intValue() * num;
            } else {
                logger.error("踏春游记-出行道具未找到：{}", goodsId);
            }
        }
        return 0;
    }

    /**
     * 获取玩家积分信息
     * @param reqMsg
     * @return
     */
    public DoubleSpringProto.DoubleSpringGetScoreRespMsg.Builder getScore(DoubleSpringProto.DoubleSpringGetScoreReqMsg reqMsg) {
        DoubleSpringProto.DoubleSpringGetScoreRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringGetScoreRespMsg.newBuilder();
        for (long userId : reqMsg.getUserIdsList()) {
            DoubleSpringProto.DoubleSpringPlayerScoreData.Builder scoreItem = DoubleSpringProto.DoubleSpringPlayerScoreData.newBuilder();
            scoreItem.setUserId(userId);
            DoubleSpringUserData data = this.userDataMap.get(userId);
            if (data == null) {
                scoreItem.setScore(0);
                scoreItem.setOutingFlag(false);
            } else {
                scoreItem.setScore(data.getTotalScore());
                scoreItem.setOutingFlag(data.getPartnerUserId() > 0);
            }
            respMsg.addPlayerScoreData(scoreItem);
        }
        respMsg.setRet(0);
        return respMsg;
    }

    /**
     * 查询玩家
     * @param activityId
     * @param userId
     * @param serverId
     * @param queryUserAliasId
     * @param queryServerId
     */
    public void queryUser(int activityId, long userId, long serverId, long queryUserAliasId, long queryServerId) {
        //查询玩家基础信息
        Map<Long, CrossUserBaseInfo> map = CrossUserMgr.getUserAliasIdUserBaseInfoMap(queryServerId,false);
        if (map != null && map.containsKey(queryUserAliasId)) {
            CrossUserBaseInfo crossUserBaseInfo = map.get(queryUserAliasId);
            DoubleSpringProto.DoubleSpringQueryUserRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringQueryUserRespMsg.newBuilder();
            //积分数据
            DoubleSpringUserData doubleSpringUserData = this.userDataMap.get(crossUserBaseInfo.getUserId());
            //构建PB
            DoubleSpringProto.DoubleSpringPlayerData.Builder playerData = CrossDoubleSpringActivityPb.buildDoubleSpringPlayerData(crossUserBaseInfo.getUserId(), doubleSpringUserData, crossUserBaseInfo.getUserBaseInfo());
            respMsg.setRet(0);
            respMsg.setPlayerData(playerData);
            //pb返回
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_DOUBLE_SPRING_QUERY_USER, respMsg));
        } else {
            //玩家信息不存在，去区服查询
            DoubleSpringProto.DoubleSpringCrossQueryUserReqMsg.Builder builder = DoubleSpringProto.DoubleSpringCrossQueryUserReqMsg.newBuilder();
            builder.setActivityId(activityId);
            builder.setUserId(userId);
            builder.setServerId(serverId);
            builder.setQueryUserAliasId(queryUserAliasId);
            builder.setQueryUserId(0);
            builder.setQueryServerId(queryServerId);
            MessageHelper.sendPacket(queryServerId, 0, YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_CROSS_QUERY_USER_BASE, builder));
        }
    }

    /**
     * 查询玩家-区服返回
     * @param reqMsg
     */
    public void queryUserServerReturn(DoubleSpringProto.DoubleSpringServerToCrossQueryUserReturnReqMsg reqMsg) {
        //存储userBase
        if(reqMsg.hasPlayerBaseData()){
            UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
            CrossUserMgr.updateUserBaseInfo(reqMsg.getQueryUserId(), userBaseInfo);
        }
        //如果 userId 或者 serverId 为0。则不需要返回玩家信息给客户端，单纯是为了存储玩家数据到跨服
        if (reqMsg.getUserId() == 0 || reqMsg.getServerId() == 0) {
            return;
        }
        //查询玩家基础信息
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(reqMsg.getQueryUserId());
        DoubleSpringProto.DoubleSpringQueryUserRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringQueryUserRespMsg.newBuilder();
        //积分数据
        DoubleSpringUserData doubleSpringUserData = this.userDataMap.get(reqMsg.getQueryUserId());
        //构建PB
        DoubleSpringProto.DoubleSpringPlayerData.Builder playerData = CrossDoubleSpringActivityPb.buildDoubleSpringPlayerData(reqMsg.getQueryUserId(), doubleSpringUserData, userBaseInfo);
        respMsg.setRet(0);
        respMsg.setPlayerData(playerData);
        //pb返回
        MessageHelper.sendPacket(reqMsg.getServerId(), reqMsg.getUserId(), YanQuMessageUtils.buildMessage(Protocol.U_DOUBLE_SPRING_QUERY_USER, respMsg));
    }

    /**
     * 同步玩家双人同行数据
     * @param userId
     * @param serverId
     * @param updateConditionFlag 是否更新condition
     */
    public void syncUserData(long userId, long serverId, boolean updateConditionFlag) {
        DoubleSpringProto.DoubleSpringUserDataTwoSyncMsg.Builder builder = DoubleSpringProto.DoubleSpringUserDataTwoSyncMsg.newBuilder();
        builder.setActivityId(this.doubleSpringActivityConfig.getActivityInfo().getActivityId());
        //获取玩家数据
        DoubleSpringUserData userData = this.userDataMap.get(userId);
        UserBaseInfo userBaseData = CrossUserMgr.getUserBaseInfo(userId);
        DoubleSpringProto.DoubleSpringPlayerData.Builder userBuild = CrossDoubleSpringActivityPb.buildDoubleSpringPlayerData(userId, userData, userBaseData);
        builder.setUserData(userBuild);
        //伙伴数据
        if (userData != null && userData.getPartnerUserId() > 0) {
            DoubleSpringUserData partnerData = this.userDataMap.get(userData.getPartnerUserId());
            UserBaseInfo partnerBaseData = CrossUserMgr.getUserBaseInfo(userData.getPartnerUserId());
            DoubleSpringProto.DoubleSpringPlayerData.Builder partnerBuild = CrossDoubleSpringActivityPb.buildDoubleSpringPlayerData(userData.getPartnerUserId(), partnerData, partnerBaseData);
            builder.setPartnerData(partnerBuild);
        }
        //邀请,被邀请数据
        builder.setInviteSize(this.userInviteDataMap.get(userId) == null ? 0 : this.userInviteDataMap.get(userId).size());
        builder.setBeInviteSize(this.userBeInviteDataMap.get(userId) == null ? 0 : this.userBeInviteDataMap.get(userId).size());
        if (updateConditionFlag) {
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_SYNC_SCORE_FROM_CROSS, builder));
        } else {
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_DOUBLE_SPRING_USER_DATA_TWO_SYNC, builder));
        }
    }
}
